package com.example.musicbox.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * @author : zhy
 * @since : 2022/8/2
 * 创建允许异步执行的线程池
 */
@Configuration
@EnableAsync
public class ThreadPoolEx {

    @Bean("taskExecutor")
    public Executor asyncServiceExecutor()
    {
        //根据源码来看，这个ThreadPoolTaskExecutor是不具有初始化信息的线程池创建，
        // 接下来对该线程池进行初始化信息
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //设置最大核心线程数
        executor.setCorePoolSize(5);
        //设置最大线程数
        executor.setMaxPoolSize(10);
        //无边界等待队列
        executor.setQueueCapacity(Integer.MAX_VALUE);
        //线程最大存活时间
        executor.setKeepAliveSeconds(60);
        //设置线程池中线程的前缀名称
        executor.setThreadNamePrefix("xyc_");
        //设置完成任务之后关闭宣传册
        executor.setWaitForTasksToCompleteOnShutdown(true);
        //对线程池进行初始化
        executor.initialize();
        //返回信息初始化的线程池
        return executor;

    }

}
